<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <el-tabs v-model="activeName" type="card" class="file-tabs" @tab-click="handleClick">
            <el-tab-pane :label="label.label" :name="label.value" v-for="(label, index) in statusList" :key="index"></el-tab-pane>
        </el-tabs>
        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: '城市名称/城市编码' })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table />

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { provide, ref } from 'vue'
import baTableClass from '/@/utils/baTable'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import type { TabsPaneContext } from 'element-plus'

defineOptions({
    name: 'routine/city',
})

const activeName = ref('')
const statusList: any[] = ref([])

const { t } = useI18n()
const baTable = new baTableClass(
    new baTableApi('city/'),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('Id'), prop: 'id', align: 'center', operator: '=', operatorPlaceholder: t('Id'), width: 70 },
            {
                label: '城市',
                prop: 'name',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                width: 150,
            },
            {
                label: t('State'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                operator: 'IN',
                custom: { 0: 'danger', 1: 'success' },
                replaceValue: { 0: t('Disable'), 1: t('Enable') },
            },
            {
                label: '默认',
                prop: 'is_default',
                align: 'center',
                render: 'switch',
                operator: 'IN',
                custom: {
                    activeValue: 1,
                    inactiveValue: 0,
                    activeColor: '#13ce66',
                    inactiveColor: '#dcdfe6',
                },
            },
            { label: t('Create time'), prop: 'createtime', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE' },
            {
                label: t('Operate'),
                align: 'center',
                render: 'buttons',
                buttons: defaultOptButtons(['edit', 'delete']),
                operator: false,
            },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: {
            status: 1,
        },
    },
    {},
    {
        getEditData({ res }) {
            if (res && res.data) {
                baTable.form.items = res.data
            }
        },
        getData({ res }) {
            if (res && res.data) {
                statusList.value = [{ label: '全部', value: '' }]
                const status = res.data.statusList
                const tempResult = Object.entries(status).map(([k, v]) => ({
                    label: v, // 原对象的键作为新对象的 key 字段
                    value: k, // 原对象的值作为新对象的 value 字段
                }))
                statusList.value.push.apply(statusList.value, tempResult)
            }
        },
    }
)

// 点击分类标签切换查询条件
const handleClick = (tab: TabsPaneContext, event: Event) => {
    baTable.comSearch.form.status = tab.props.name
    baTable.onTableAction('com-search', {})
}


provide('baTable', baTable)

baTable.mount()
baTable.getData()
</script>

<style scoped lang="scss">
.file-tabs :deep(.el-tabs__header) {
    border-bottom: none;
    margin-bottom: 0;
}
.file-tabs :deep(.is-active) {
    background-color: #fff;
}
</style>
